package com.tw.utils;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.pig.data.Tuple;

import com.jd.ipservice.BinaryIpService;
import com.jd.ipservice.IpInfo;


public class LogUtil {
	private static SimpleDateFormat sdf0 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	private static SimpleDateFormat sdf1 = new SimpleDateFormat("MMMMddHH:mm:ssyyyyy", Locale.ENGLISH);
	private static SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static String TEXT = "htm,html,php,asp,shtm,shtml,aspx,xml,xhtml,xsl,cfm,htx,htmls,phtml,jsp,php3,txt,wml,lhtml,pl,cgi,acgi,srch,qry,js,css";
	
	public static String IMAGE = "jpg,png,gif,jpeg,bmp,ico,icon"; 
	
	public static String DOWNLOADS = "exe,rar,zip,tar,gz,iso,img,dll,pdf,mid,mp2,mp3,mp4,wma,smil"; 
	
	public static String VIEW = "swf,avi,wav,mov,m4v,wmv,asf,mpg,3gp,mpeg,ram";
	
	public static BinaryIpService ipService;
	static{
		try {
			ipService = new BinaryIpService();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public static Tuple parseLog(Tuple tuple,String line) throws ParseException, MalformedURLException{
		String[] strs = StringUtils.split(line, "\t");
		if(strs==null||strs.length!=31)
			return null;
		String oraingeTime =  strs[0];
		String times[] = StringUtils.split(oraingeTime," ") ;
		String timevalue = times[1]+times[2]+times[3]+times[5];
		Date date = sdf1.parse(timevalue);
		String time = sdf0.format(truncate30Minute(date));
		//时间精确到半小时
		tuple.append(time);
		//时间精确到秒
		tuple.append(sdf2.format(date));
		String domain = null;
		String getHead = strs[30];
		if(getHead.indexOf("Host:")>-1){
			domain = StringUtils.trim(StringUtils.substringBetween(getHead, "Host:", ","));
			if(StringUtils.isEmpty(domain))
				domain = StringUtils.trim(StringUtils.substringBetween(getHead, "Host:", "]"));
		}else{
			domain = "N";
		}
		//域名
		tuple.append(domain);
		
		String getUrl = strs[29];
		String paths[] =  StringUtils.split(getUrl," ");
		String urlValue = paths[1];
		String requestUrl = null;
		if(!domain.equals("N")){
			requestUrl = domain +urlValue;
		}else{
			requestUrl = urlValue;
		}
		//requestUrl
		tuple.append(requestUrl);
		
		String srcip = strs[16];
		IpInfo srcipinfo = ipService.getIpInfo(srcip);
		//srcip
		tuple.append(srcip);
		//src省
		tuple.append(srcipinfo.getProvince());
		//srcIsp
		tuple.append(srcipinfo.getIsp());
		//srciprange
		tuple.append(StringUtils.substringBeforeLast(srcip, ".")+".0/24");
		
		String destip = strs[17];
		IpInfo destinfo = ipService.getIpInfo(destip);
		//destip
		tuple.append(destip);
		//dest省
		tuple.append(destinfo.getProvince());
		//destIsp
		tuple.append(destinfo.getIsp());
		//destiprange
		tuple.append(StringUtils.substringBeforeLast(destip, ".")+".0/24");	
		
		String judge = StringUtils.substringAfterLast(urlValue, ".");
		 if(IMAGE.contains(judge)){
			tuple.append("1");
		}else if(DOWNLOADS.contains(judge)){
			tuple.append("2");
		}else if(VIEW.contains(judge)){
			tuple.append("3");
		}else{
			//文本
			tuple.append("0");
		}
		String userAgent = null;
		if(getHead.indexOf("User-Agent:")>-1){
			 userAgent = StringUtils.substringBeforeLast(StringUtils.trim(StringUtils.substringBetween(getHead, "User-Agent:", ":")), ",");
				if(StringUtils.isEmpty(userAgent))
					userAgent = StringUtils.trim(StringUtils.substringBetween(getHead, "User-Agent:", "]"));
		}else{
			userAgent = "N";
		}
		String os = UserAgentUtil.getOperatingSystem(userAgent);
		//操作系统信息
		tuple.append(os);
		String browser = UserAgentUtil.getUserAgentBrowser(userAgent);
		//浏览器信息
		tuple.append(browser);
		
		String referer = null;
		if(getHead.indexOf("Referer:")>-1){
			referer = StringUtils.substringBeforeLast(StringUtils.trim(StringUtils.substringBetween(getHead, "Referer:", ":")), ",");
				if(StringUtils.isEmpty(referer))
					referer = StringUtils.trim(StringUtils.substringBetween(getHead, "Referer:", "]"));
		}else{
			referer = "N";
		}
		//referer信息
		tuple.append(referer);
		String phone = null;
		String tempAgent = StringUtils.substringBetween(userAgent, "(", ")");
		if(StringUtils.isEmpty(tempAgent)){
			phone = "N";
		}else{
			if(tempAgent.indexOf("Linux;")>-1){
				if(tempAgent.indexOf("Build")>-1){
					phone = StringUtils.trim(StringUtils.substringBefore(StringUtils.substringAfterLast(tempAgent, ";"),"Build"));
				}else{
					phone = tempAgent;
				}
			}else{
				phone = "N";
			}
		}
		//手机型号
		tuple.append(phone);
		return tuple;
	}
	private static Date truncate30Minute(Date date) {
		return new Date(date.getTime() / 1800000 * 1800000);
	}
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws IOException, ParseException {
		Date date = new Date();
		System.out.println(sdf0.format(truncate30Minute(date)));
		String destip = "114.112.34.101";
		System.out.println(StringUtils.substringBeforeLast(destip, ".")+".0/24");
		List<String> list = FileUtils.readLines(new File("D:/other/data-eth0-201307261623"));
		List<String> listUA = new ArrayList<String>();
		for(String line : list){
			String[] strs = StringUtils.split(line, "\t");
			String getHead = strs[30];
			String userAgent = null;
			if(getHead.indexOf("User-Agent:")>-1){
				 userAgent = StringUtils.substringBeforeLast(StringUtils.trim(StringUtils.substringBetween(getHead, "User-Agent:", ":")), ",");
					if(StringUtils.isEmpty(userAgent))
						userAgent = StringUtils.trim(StringUtils.substringBetween(getHead, "User-Agent:", "]"));
			}else{
				userAgent = "N";
			}
			String phone = null;
			String tempAgent = StringUtils.substringBetween(userAgent, "(", ")");
			if(StringUtils.isEmpty(tempAgent)){
				phone = "N";
			}else{
				if(tempAgent.indexOf("Linux;")>-1){
					if(tempAgent.indexOf("Build")>-1){
						phone = StringUtils.trim(StringUtils.substringBefore(StringUtils.substringAfterLast(tempAgent, ";"),"Build"));
					}else{
						phone = tempAgent;
					}
				}else{
					phone = "N";
				}
			}
			listUA.add(phone);
//			String os = UserAgentUtil.getOperatingSystem(userAgent);
//			String browser = UserAgentUtil.getUserAgentBrowser(userAgent);
		}
		FileUtils.writeLines(new File("D:/other/phone.txt"), listUA);
	}
}
